package com.zhang.zxx.boot.controller.ruletest;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * RuleTestController:规则控制层
 *
 * @author zhangxiaoxiang
 * @date 2022/02/24
 */
@RestController("/rule")
@RequiredArgsConstructor
public class RuleTestController {
    private final List<RuleInterface> ruleInterfaces;

    @RequestMapping("/test")
    public RuleVO test() {
        //数据库查询出规则[根据请求参数]
        List<LearningEntity> dbList = new ArrayList<>();
        //查询出学员的课程,考试
        Map<String, List<RuleData>> courseBOList = new HashMap<>(16);
        Map<String,List<RuleData>> examBOList = new HashMap<>(16);
        for (RuleInterface ruleInterface : ruleInterfaces) {
            //课程和考试都满足[强制非强制一并计算]
            if (ruleInterface.apply(dbList, courseBOList).getCanDirectIn() && ruleInterface.apply(dbList, examBOList).getCanDirectIn()) {
                //课程和课时都满足,直接跳
                return RuleVO.canDirectInVO();
            }
            //课时或者考试不合格的直接返回
            List<String> courseList = ruleInterface.apply(dbList, courseBOList).getMessageList();
            List<String> examList = ruleInterface.apply(dbList, examBOList).getMessageList();
            List<String> allMsgList = new ArrayList<>();
            allMsgList.addAll(courseList);
            allMsgList.addAll(examList);
            return RuleVO.canNotDirectInVO(allMsgList, "云课堂地址");
        }
        //    默认返回直接进入
        return RuleVO.canDirectInVO();
    }
}
